/**
 * 
 */
package smallChanges.statements;

import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

import smallChanges.Tokenization.TokenSet;

/**
 * Simple container for a block of statements
 * @author jspacco
 *
 */
/**
 * @author jspacco
 *
 */
public class StatementList extends Statement implements Iterable<Statement> 
{
    /**
     * Singleton for an emtpy statement list
     */
    public static final StatementList EMPTY_STATEMENT_LIST=new StatementList() {
        @Override
        public void addStatement(Statement stmt)
        {
            throw new UnsupportedOperationException("Can't add to EMPTY_STATEMENT_LIST");
        }
    };
    
	private TreeSet<Statement> statements=new TreeSet<Statement>();
	
    private StatementList() {
        super(null);
    }
	
	public StatementList(TokenSet allTokensInSet) {
		super(STATEMENT_LIST, allTokensInSet);
	}
	
	public int size() {
		return statements.size();
	}
	
	public String getImage()
	{
	    // TODO is this the best thing to do here?  Shouldn't this just turn into
	    // one big sequence of tokens?
		String image="statement list with " +size()+ " statements";
        return image;
	}
    
     List<Statement> flattenHelper(List<Statement> list) {
         for (Statement s : this) {
             s.flattenHelper(list);
         }
         return list;
     }
	
	public String toString()
	{
		return toStringHelper(0);
	}

	String toStringHelper(int depth)
    {
        StringBuffer result=new StringBuffer();
        result.append(makePadding(depth)+"{ " +getStmtType()+ " with " +size()+ 
        		" stmts at " +getCoords()+"\n");
        for (Statement s : this) {
        	    result.append(s.toStringHelper(depth+1)+"\n");
        }
        result.append(makePadding(depth)+"}\n");
        return result.toString();
    }
	
	
    
    public void addStatement(Statement stmt) {
		statements.add(stmt);
	}
	
	public Iterator<Statement> iterator() {
		return statements.iterator();
	}
}
